package com.sendi.mobilework.adapter;

import android.content.Context;
import android.content.Intent;
import android.net.Uri;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.Button;
import android.widget.LinearLayout;
import android.widget.SectionIndexer;
import android.widget.TextView;
import android.widget.Toast;

import com.sendi.mobilework.R;
import com.sendi.mobilework.datamodel.ContactBook;

import java.util.List;

/**
 * Created by Administrator on 2017/3/25.
 */

public class ContactAdapter extends BaseAdapter implements SectionIndexer {
    private List<ContactBook>mModelList;
    private Context mContext;
    private ContactBook mCotent;

    public ContactAdapter(Context context, List<ContactBook>list){
        mContext=context;
        mModelList=list;
//        Log.i("mModelList", "SortAdapter: "+mModelList.size());
    }
    /**
     * 当ListView数据发生变化时,调用此方法来更新ListView
     *
     * @param list
     */
    public void updateListView(List<ContactBook> list) {
        mModelList = list;
        notifyDataSetChanged();
    }

    @Override
    public int getCount() {
        return mModelList.size();
    }

    @Override
    public ContactBook getItem(int position) {
        return mModelList.get(position);
    }

    @Override
    public long getItemId(int position) {
        return position;
    }

    @Override
    public View getView(final int position, View convertView, ViewGroup parent) {
        ViewHolder holder=null;
       mCotent =mModelList.get(position);
        if (convertView==null){
           holder=new ViewHolder();
            convertView= LayoutInflater.from(mContext).inflate(R.layout.contact_book_item,null);
            holder.tvName= (TextView) convertView.findViewById(R.id.tv_name);
            holder.btCall= (Button) convertView.findViewById(R.id.bt_call);//呼叫按钮
            holder.tvNumber= (TextView) convertView.findViewById(R.id.tv_number);
            convertView.setTag(holder);
            holder.tvLetter= (TextView) convertView.findViewById(R.id.tv_catagory);
            holder.linearLayout= (LinearLayout) convertView.findViewById(R.id.ll_item);
        }else {
            holder= (ViewHolder) convertView.getTag();
        }
        int section=getSectionForPosition(position);
        if (position==getPositionForSection(section)){
            holder.linearLayout.setVisibility(View.VISIBLE);
            holder.tvLetter.setText(mCotent.getSortLetters());
        }else {
            holder.linearLayout.setVisibility(View.GONE);
        }

        holder.tvName.setText(mModelList.get(position).getcName());
        holder.tvNumber.setText(mModelList.get(position).getNumber());

        holder.btCall.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                //呼叫
                Toast.makeText(mContext, "呼叫:"+mModelList.get(position).getNumber(), Toast.LENGTH_SHORT).show();
                Intent intent=new Intent(Intent.ACTION_DIAL);
                intent.setData(Uri.parse("tel:"+mModelList.get(position).getNumber()));
                mContext.startActivity(intent);
            }
        });
        return convertView;
    }

    final static class ViewHolder{
        TextView tvLetter;
        TextView tvName;
        TextView tvNumber;
        LinearLayout linearLayout;
        Button btCall;
    }

    @Override
    public Object[] getSections() {
        return null;
    }

    @Override
    public int getPositionForSection(int sectionIndex) {
        for (int i=0;i<getCount();i++){
            String sortStr=mModelList.get(i).getSortLetters();
            char firstChar=sortStr.toUpperCase().charAt(0);
            if (firstChar==sectionIndex){
                return i;
            }
        }
        return -1;
    }

    @Override
    public int getSectionForPosition(int position) {
        return mModelList.get(position).getSortLetters().charAt(0);
    }
}
